﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PhysicsSimulator
{
    public enum DotDirection
    {
        TOP_BOTTOM=0,
        RIGHT_LEFT=1,
        BOTTOM_TOP=2,
        LEFT_RIGHT=3,
    }
    class CDotLight : CGameObject
    {
        private double _timerCounter = 0;
        private const int NUMOFDOT = 27;
        private float offsetX = 5.5f;
        private float offsetY = 5.5f;
        private int _id = 0;
        private Vector2 _position = Vector2.Zero;
        private DotDirection _direction = DotDirection.BOTTOM_TOP;
        private CSprite _dotSprite = null;
        private Color _opacity = Color.White;

        public CDotLight(SpriteBatch spriteBatch,int id) : base(spriteBatch)
        {
            _id = id;
        }

        public override void Init(Microsoft.Xna.Framework.Content.ContentManager contentManager)
        {
            base.Init(contentManager);
            _dotSprite = new CSprite(contentManager.Load<Texture2D>("Images/DotLight/dot-light-"+_id.ToString()));
        }

        public override void Update(GameTimerEventArgs timer)
        {
            base.Update(timer);
            _timerCounter += timer.ElapsedTime.TotalSeconds;
            if (_timerCounter > 0.1f)
            {
                Vector2 dir = GetVectorOfDirection(_direction);
                _timerCounter = 0;
                _position.X += dir.X * 2 * offsetX;
                _position.Y += dir.Y * 2 * offsetY;
            }

            switch (_direction)
            {
                case DotDirection.BOTTOM_TOP:
                    if(_position.Y < -(50 + NUMOFDOT*offsetY*2))
                    {
                        RandomRirection();
                    }
                    break;
                case DotDirection.LEFT_RIGHT:
                    if (_position.X > 530 + NUMOFDOT * offsetX * 2)
                    {
                        RandomRirection();
                    }
                    break;
                case DotDirection.RIGHT_LEFT:
                    if (_position.X < -(50 + NUMOFDOT * offsetX * 2))
                    {
                        RandomRirection();
                    }
                    break;
                case DotDirection.TOP_BOTTOM:
                    if (_position.Y > 800 + (50 + NUMOFDOT * offsetY * 2))
                    {
                        RandomRirection();
                    }
                    break;
            }

        }

        public override void Draw(GameTimerEventArgs timer)
        {
            base.Draw(timer);
            _opacity.R = 255;
            _opacity.G = 255;
            _opacity.B = 255;
            _opacity.A = 255;
            SpriteBatch.Draw(_dotSprite.Texture,_position,null,_opacity,0,_dotSprite.Origin,1,SpriteEffects.None,0);
            Vector2 subposition = new Vector2(_position.X, _position.Y);
            Vector2 vdir = GetVectorOfDirection(_direction);
            for (int i = 0; i < NUMOFDOT-1; i++)
            {
                subposition.X -= 2*vdir.X * offsetX;
                subposition.Y -= 2*vdir.Y * offsetY;
                _opacity.R = _opacity.G = _opacity.B =_opacity.A = (byte)(255*(NUMOFDOT - i)/NUMOFDOT);
                SpriteBatch.Draw(_dotSprite.Texture, subposition, null, _opacity, 0, _dotSprite.Origin, 1, SpriteEffects.None, 0);
            }
            subposition.X -= 2 * vdir.X * offsetX;
            subposition.Y -= 2 * vdir.Y * offsetY;
            _opacity.R = _opacity.G = _opacity.B = _opacity.A = (byte)(1);
            SpriteBatch.Draw(_dotSprite.Texture, subposition, null, _opacity, 0, _dotSprite.Origin, 1, SpriteEffects.None, 0);
        }

        private Vector2 GetVectorOfDirection(DotDirection direction)
        {
            switch (direction)
            {
                case DotDirection.BOTTOM_TOP:
                    return new Vector2(0,-1);
                case DotDirection.TOP_BOTTOM:
                    return new Vector2(0,1);
                case DotDirection.LEFT_RIGHT:
                    return new Vector2(1,0);
                default:
                    return new Vector2(-1,0);
            }
        }

        public void RandomRirection()
        {
            int dir = RandomHelper.Random.Next(0, 4);
            _direction = (DotDirection)dir;
            switch (_direction)
            {
                case DotDirection.BOTTOM_TOP:
                    _position.Y = 800 + 2 * offsetY;
                    _position.X = offsetX * (RandomHelper.Random.Next(1, (int)(480.0f/offsetX)));
                    break;
                case DotDirection.LEFT_RIGHT:
                    _position.X = -2 * offsetX;
                    _position.Y = offsetY * (RandomHelper.Random.Next(1, (int)(800.0f / offsetY)));
                    break;
                case DotDirection.RIGHT_LEFT:
                    _position.X = 480 + 2 * offsetX;
                    _position.Y = offsetY * (RandomHelper.Random.Next(1, (int)(800.0f / offsetY)));
                    break;
                case DotDirection.TOP_BOTTOM:
                    _position.Y = -2 * offsetY;
                    _position.X = offsetX * (RandomHelper.Random.Next(1, (int)(480.0f/offsetX)));
                    break;
            }
        }
    }
}
